/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.expression;


import java.util.List;

import dex.compiler.model.base.ParseNode;
import dex.compiler.model.base.Place;


/**
 * A Dex expression.
 */
public abstract class Expression extends ParseNode {

	
	/**
	 * Constructs a new Expression node that originated at the given place.
	 * 
	 * @param place  The place where the expression originated
	 */
	public Expression(Place place) {
		super(place);
	}
	

	/**
	 * Returns this expression expressed as Dex code.
	 * Expressions that can be represented in more than one way
	 * by Dex code can return any valid form.
	 * 
	 * @return  some valid Dex code for this expression
	 */
	public abstract String toString();
	

	/**
	 * Returns the list of expressions as a comma-separated list of strings.
	 * 
	 * @param list  the list of expressions
	 * @return  the comma-separated string
	 */
	public static String listToString(List<? extends Expression> list) {
		if (list.isEmpty()) {
			return "";
		}
		StringBuilder result = new StringBuilder();
		result.append(list.get(0));
		for (int i = 1; i < list.size(); i++) {
			result.append(", ").append(list.get(i));
		}
		return result.toString();
	}


}
